package com.xywl.escort.view.message

import android.os.Bundle
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.trello.rxlifecycle2.LifecycleTransformer
import com.xywl.escort.R
import com.xywl.escort.adapter.MessageListAdapter
import com.xywl.escort.base.BaseActivity
import com.xywl.escort.bean.MessageResp
import com.xywl.escort.bean.MessageResp.DataBean.ListBean
import com.xywl.escort.common.PreferenceKeys.USER_ID
import com.xywl.escort.utils.RxSPTool
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_message.*
import java.util.*
import javax.inject.Inject

@Route(path = "/xy/message")
class MessageActivity : BaseActivity(), MessageContract.View, SwipeRefreshLayout.OnRefreshListener {
    private lateinit var mAdapter: MessageListAdapter
    private val mList = ArrayList<ListBean>()

    private var pageNum = 1
    private var isEnd: Boolean = false

    @Inject
    lateinit var mPresenter: MessageContract.Presenter

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_message)

        initView()
    }

    private fun initView() {
        showBackButton()
        setTitleText(R.string.message)

        mAdapter = MessageListAdapter(R.layout.item_message_list, mList)
        recycler_message.layoutManager = LinearLayoutManager(this)
        recycler_message.adapter = mAdapter

        val emptyView = LayoutInflater.from(this).inflate(R.layout.layout_empty_view, null)
        val tvEmpty = emptyView.findViewById<TextView>(R.id.tv_empty)
        tvEmpty.text = "暂无消息"
        mAdapter.emptyView = emptyView

        swipeLayout.setOnRefreshListener(this)
        swipeLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.aid_prompt_color, R.color.colorBackground)
        mAdapter.setOnLoadMoreListener({
            swipeLayout.isEnabled = false
            recycler_message.postDelayed({
                swipeLayout.isEnabled = false
                if (isEnd) {
                    //数据全部加载完毕
                    mAdapter.loadMoreEnd()
                    swipeLayout.isEnabled = true
                } else {
                    //成功获取更多数据
                    val userId = RxSPTool.getString(this, USER_ID)
                    mPresenter.loadMessage(userId, pageNum)
                }
            }, 200)
        }, recycler_message)

        mAdapter.setOnItemClickListener { _, _, position ->
            val bean = mList[position]
            ARouter.getInstance().build("/xy/message/detail").withObject("bean", bean).navigation()
        }
    }

    private fun initData() {
        pageNum = 1
        mPresenter.takeView(this)
        val userId = RxSPTool.getString(this, USER_ID)
        mPresenter.loadMessage(userId, pageNum)
    }

    override fun onResume() {
        super.onResume()
        initData()
    }

    override fun isActive(): Boolean {
        return false
    }

    override fun <T> bindToLife(): LifecycleTransformer<T> {
        return this.bindToLifecycle()
    }

    override fun onRefresh() {
        mAdapter.setEnableLoadMore(false)
        pageNum = 1
        val userId = RxSPTool.getString(this, USER_ID)
        mPresenter.loadMessage(userId, pageNum)
    }

    override fun updateMessage(dataBean: MessageResp.DataBean) {
        swipeLayout.isRefreshing = false
        progress_bar.visibility = View.GONE
        recycler_message.visibility = View.VISIBLE
        if (pageNum == 1) {
            mList.clear()
        }
        if (pageNum >= dataBean.pages) {
            isEnd = true
        } else {
            isEnd = false
            pageNum += 1
        }
        mList.addAll(dataBean.list)
        mAdapter.setNewData(mList)
        mAdapter.loadMoreComplete()
        swipeLayout.isEnabled = true
    }

    override fun loadMessageFailure(msg: String?) {
        swipeLayout.isRefreshing = false
        progress_bar.visibility = View.GONE
        recycler_message.visibility = View.VISIBLE
    }
}
